package cn.train.controler;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.train.model.CheXiang;
import cn.train.service.AddOrderTableService;
import cn.train.service.QueryCXService;
@WebServlet("/UserZhiFuServlet")
public class UserZhiFuServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String oid = req.getParameter("oid");
		String carname = req.getParameter("carname");
		String zuotype = req.getParameter("zuotype");		
		String cdate = req.getParameter("cdate");
		String time = req.getParameter("time");
		
		System.out.println(oid+","+carname+","+zuotype+","+cdate);
		AddOrderTableService ats = new AddOrderTableService();
		SimpleDateFormat adf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat adf = new SimpleDateFormat("yyyy-MM-dd");
		PrintWriter out=resp.getWriter();
		java.util.Date date2;
		try {
			date2 = adf1.parse(cdate+" "+time);
			System.out.println("======="+date2);
			java.util.Date dat=new java.util.Date();
			System.out.println("dat===="+dat);
			long time1 = dat.getTime();
			System.out.println("11111111111111"+time1);
	        long time2 = date2.getTime();
	        System.out.println("222222222222"+time2);
	        long diff = time1 - time2;
	        System.out.println("333333333333"+diff);
	        if(diff>0){
            	System.out.println("133135151321321321321321321");
            	out.print("该列车已经出发，订单失效！！！");
                 return;
            
	        }else{

        		AddOrderTableService addts=new AddOrderTableService();
        		QueryCXService queryche=new QueryCXService();
        		
        		int flag = ats.updatezhuangtaiState(Integer.parseInt(oid));
        		
        		if(flag==1){
        			String chexiangzuohao="";
        		 	try {
        				java.util.Date date1 = adf.parse(cdate);
        				java.sql.Date sqlDate=new java.sql.Date(date1.getTime());
        				
        				//先查票数piaoshu
        				int piaoshu=addts.HuoQuPiao(carname, zuotype, sqlDate);
        				//再分车厢车座
        				//商务座的车厢和座号
        				System.out.println("单程票数"+piaoshu);
        				for(int i=1;i<=1;i++){
        					
        				if("商务座".equals(zuotype)){
        				
        					
        						piaoshu++;
        			
        					List<CheXiang> listshang=queryche.QuerySwid(piaoshu, piaoshu);
        					req.setAttribute("listshang", listshang);
        					for(CheXiang c:listshang){
        						chexiangzuohao+=c.getChexianghao()+"车"+c.getZuohao()+"座-";
        						System.out.println(c.getChexianghao()+";"+c.getZuohao());
        					}

        					
        				}
        				//普通座的车厢和座号
        				if("普通座".equals(zuotype)){
        					
        						piaoshu++;
        					
        					List<CheXiang> listpu=queryche.QueryPtid(piaoshu, piaoshu);
        			
        				   for(CheXiang c:listpu){
        					   chexiangzuohao+=c.getChexianghao()+"车"+c.getZuohao()+"座-";
        						System.out.println(c.getChexianghao()+";"+c.getZuohao());
        					}

        				  
        				   req.setAttribute("listpu", listpu);
        				}
        				//无座的车厢
        				if("无座".equals(zuotype)){
        					
        						piaoshu++;
        					
        					int wuzuo=queryche.QueryZhid(piaoshu);
        					chexiangzuohao+=wuzuo+"-";
        				  req.setAttribute("wuzuo", wuzuo);
        				  System.out.println(wuzuo);

        				}
        				}
        				 chexiangzuohao=chexiangzuohao.substring(0,chexiangzuohao.length()-1);
        				//req.setAttribute("chexiangzuohao", chexiangzuohao);
        				System.out.println("单程"+chexiangzuohao);
        				//System.out.println("一共有"+shu);
        				//后改数据库中票量
        				int flag1=addts.panDuanPiao(zuotype, 1,sqlDate,carname);
        				  System.out.println(flag1);				  
        				  PrintWriter out1 =resp.getWriter();
        				  if(flag1==1){
        					  out1.print("支付成功，您的车厢座号是："+chexiangzuohao+"，请到临近售票点取票。");
        				  }
        				  
        			} catch (ParseException e) {
        				// TODO Auto-generated catch block
        				e.printStackTrace();
        			}
        		}
            }
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
	}
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	doGet(req, resp);
    }	
}
